import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grocery_app_demo/core/utils/global.dart';
import 'package:grocery_app_demo/routes/app_routes.dart';

class SettingCtrl extends GetxController {
  final GlobalKey<NavigatorState> logoutDialogKey = GlobalKey();

  TextEditingController hookWeightCtrl = TextEditingController();

  Rx<WeightUnit> weightUnit = Rx(WeightUnit.jin);
  RxDouble hookWeight = 0.0.obs;

  @override
  void onReady() {
    hookWeightCtrl.text =
        Global.getDouble(SpKey.hookWeight, defaultValue: 0.0)!.toString();
  }

  void changeWeightUnit(WeightUnit? unit) {
    weightUnit.value = unit ?? WeightUnit.jin;
    Global.weightUnit = weightUnit.value;
  }

  void saveHookWeight() {
    var hookWeight = double.parse(hookWeightCtrl.value.text);
    Global.setDouble(SpKey.hookWeight, hookWeight);
    Get.snackbar("保存成功".tr, "");
  }

  void logout(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('退出登陆'.tr),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text('取消'.tr),
          ),
          TextButton(
            onPressed: () {
              Global.token = '';
              Get.offAndToNamed(AppRoutes.loginPage);
            },
            child: Text('确定'.tr),
          ),
        ],
      ),
    );
  }
}
